<template>
    <addEditTeacherSubjectView ref="addEditTeacherSubjectView" />
    <div class="app-table">
        <div class="app-body">
            <div class="app-search">
                <el-form :inline="true" class="demo-form-inline" v-model="form">
                    <el-form-item label="姓名" style="width:200px">
                        <el-input placeholder="请输入姓名" v-model="form.teacherName"></el-input>
                    </el-form-item>
                    <el-form-item label="部门" prop="gradeId" style="width:200px">
                        <el-select clearable v-model="form.departmentId" autocomplete="off" placeholder="请选择部门">
                            <el-option v-for="item in departmentList" :key="item.departmentId"
                                :value="item.departmentId" :label="item.departmentName"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit" @keyup.enter="searchEnter">搜索</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <el-table size="small" :data="list" border :header-row-style="{ height: '5vh' }">
                <el-table-column prop="id" label="序号" />
                <el-table-column prop="teacherNumber" label="工号" />
                <el-table-column prop="teacherName" label="人员姓名" />
                <el-table-column prop="subjectName" label="任教学科" />
                <el-table-column label="操作" width="160px">
                    <template style="display: block" #default="scope">
                        <el-button size="small" type="success" @click="editTeacherSubjectInfo(scope.row)">编辑
                        </el-button>
                        <el-button size="small" type="danger"
                            @click="deleteTeacherDepartment(scope.row.teacherNumber)">删除
                        </el-button>
                    </template>
                </el-table-column>
                <template v-slot:empty>
                    <el-empty description="暂无数据"></el-empty>
                </template>
            </el-table>
            <div style="margin-top: 10px">
                <el-config-provider :locale="locale">
                    <el-pagination :total="total" :page-size="limit" :current-page="page"
                        :page-sizes="[5, 10, 20, 30, 40, 50, 100]" layout="total, sizes, prev, pager, next, jumper"
                        @size-change="changePageSize" @current-change="changeCurrentPage">
                    </el-pagination>
                </el-config-provider>
            </div>
        </div>
        <bottomView></bottomView>
    </div>
</template>
<script>
import addEditTeacherSubjectView from '../../components/schedule/addEditTeacherSubject';
import { ElConfigProvider, ElMessageBox, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";
import bottomView from "@/components/bottom.vue";

export default {
    name: "teacherSubjectList",
    data() {
        return {
            type: 0,
            title: "",
            page: 1,
            pageSize: 10,
            total: 0,
            limit: 10,
            list: [],
            departmentList: [],
            form: {
                teacherName: "",
                departmentId: ""
            },
        };
    },
    components: {
        addEditTeacherSubjectView,
        IconPicture,
        ElConfigProvider,
        ElMessageBox,
        bottomView,
        ElMessage, Upload
    },
    setup() {
        return {
            locale: zhCn,
        };
    },
    mounted() {
        this.getTeacherSubjectList();
        this.getDepartmentList();
    },
    methods: {
        searchEnter: function (e) {
            // 使用 which 和 keyCode 属性来解决兼容问题
            var keyCode = window.event ? e.keyCode : e.which;
            var val = e.target.value;
            if (keyCode == 13 && val) {
                this.page = 1;
                this.getTeacherSubjectList();
            }
        },
        editTeacherSubjectInfo(data) {
            this.$refs.addEditTeacherSubjectView.title = '修改人员任教学科';
            this.$refs.addEditTeacherSubjectView.departmentList = this.departmentList;
            this.$refs.addEditTeacherSubjectView.teacherForm = JSON.parse(JSON.stringify(data));
            this.$refs.addEditTeacherSubjectView.dialogFormVisible = true
        },
        deleteTeacherDepartment(teacherNumber) {
            ElMessageBox.confirm("该操作不会删除老师，确定要删除该老师的学科信息吗？", "提醒", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    httpService("/pythonApi/scheduleApi/deleteTeacherSubject", "teacherNumber=" + teacherNumber, "post").then(
                        (data) => {
                            if (data.code == 200) {
                                ElMessage.success(data.message);
                                this.getTeacherSubjectList();
                            } else {
                                ElMessage.error(data.message);
                            }
                        }
                    );
                })
                .catch(() => {
                });
        },
        reloadData() {
            this.page = 1;
            this.getTeacherSubjectList()
        },
        exportExcel() {
            httpService(
                "/api/teacherSignInStatistics/exportExcel",
                {
                    "teacherNumber": this.form.teacherNumber,
                    "teacherName": this.form.teacherName,
                    "departmentName": this.form.departmentName,
                    "leaveType": this.form.leaveType,
                    "submitter": this.form.submitter,
                },
                "get"
            ).then((data) => {
                if (data.code == 200) {
                    window.open(data.msg, '_blank')
                }
            });
        },
        onSubmit() {
            this.page = 1;
            this.getTeacherSubjectList();
        },
        // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
        changePageSize(size) {
            this.pageSize = size;
            this.limit = size;
            this.getTeacherSubjectList();
        },

        // 改变页码，page：回调参数，表示当前选中的“页码”
        changeCurrentPage(page) {
            this.page = page;
            this.getTeacherSubjectList();
        },
        getTeacherSubjectList() {
            httpService(
                "/pythonApi/scheduleApi/getTeacherSubjectList",
                {
                    page: this.page,
                    limit: this.limit,
                    departmentId: this.form.departmentId ? this.form.departmentId : "",
                    teacherName: this.form.teacherName
                },
                "get"
            ).then((data) => {
                this.list = data.data;
                this.total = data.total;
                this.pageCount = data.pages;
            });
        },
        getDepartmentList() {
            httpService(
                "/pythonApi/attendanceApi/getDepartmentList",
                {
                    departmentType: 3
                },
                "get"
            ).then((data) => {
                this.departmentList = data.data;
            });
        },
    },
};
</script>
<style scoped>
.app-table {
    background: #eff3f5;
    padding: 10px;
    width: calc(100vw - 190px);
}

.app-search {}


.app-body {
    background: white;
    padding: 20px 20px 10px 20px;
    margin-bottom: 10px;
    min-height: calc(100vh - 243px);
}

.el-table {
    height: 55vh;
}

:deep() .el-table .el-table__row {
    height: 5vh !important;
}
</style>
